package cn.xopencode.userservice.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.mybatis.core.util.DeptUtil;
import cn.xopencode.userservice.convert.EvaluateConver;
import cn.xopencode.userservice.dal.mysql.dataobject.EvaluateDO;
import cn.xopencode.userservice.dal.mysql.mapper.EvaluateMapper;
import cn.xopencode.userservice.dal.mysql.mapper.UserMapper;
import cn.xopencode.userservice.manager.DeptManager;
import cn.xopencode.userservice.rpc.dept.dto.DeptTypeDTO;
import cn.xopencode.userservice.rpc.evaluate.dto.*;
import cn.xopencode.userservice.util.FunUtil;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author xiaodawei
 * @since 2021/9/22
 */
@Service
public class EvaluateService {


    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptManager deptManager;


    public EvaluateDTO getEavluate(String evaluateId) {
        EvaluateDTO evaluateDTO = null;
        EvaluateDO evaluateDO = evaluateMapper.selectById(evaluateId);
        if (null != evaluateDO) {
            evaluateDTO = EvaluateConver.INSTANCE.convert(evaluateDO);
            evaluateDTO.setEmplName(userMapper.selectById(evaluateDO.getCreatorId()).getEmplName());
        }
        return evaluateDTO;
    }


    public String CreateEvaluate(CreateEvaluateDTO createEvaluateDTO) {
        EvaluateDO evaluateDO = EvaluateConver.INSTANCE.convert(createEvaluateDTO);
        evaluateDO.setId(IdUtil.objectId());
        FunUtil.defaultValue(evaluateDO, createEvaluateDTO.getUserId(), true);
        DeptTypeDTO deptTypeDTO = deptManager.getDeptTypeDTO(createEvaluateDTO.getUserId());
        evaluateDO.setDeptTypeName1(deptTypeDTO.getDeptTypeName1());
        evaluateDO.setDeptTypeName2(deptTypeDTO.getDeptTypeName2());
        evaluateDO.setDeptTypeName3(deptTypeDTO.getDeptTypeName3());
        evaluateMapper.insert(evaluateDO);
        return evaluateDO.getId();
    }


    public PageResult<EvaluateDTO> findEvaluateList(EvaluatePageDTO evaluatePageDTO) {
        if (StrUtil.isNotBlank(evaluatePageDTO.getEmplName())) {
            evaluatePageDTO.setEmplName(SqlUtils.concatLike(evaluatePageDTO.getEmplName(), SqlLike.DEFAULT));
        }
        DeptUtil.addPercentSign(evaluatePageDTO);
        Page<EvaluateDTO> evaluateList = evaluateMapper.findEvaluateList(new Page<EvaluateDTO>(evaluatePageDTO.getPageNo(), evaluatePageDTO.getPageSize()), evaluatePageDTO);
        PageResult<EvaluateDTO> pageResult = new PageResult().setList(evaluateList.getRecords()).setTotal(evaluateList.getTotal());
        return pageResult;
    }

    public Integer updateEvaluate(List<UpdateEvaluateDTO> updateEvaluateDTOList) {
        Integer count = 0;
        for (UpdateEvaluateDTO updateEvaluateDTO : updateEvaluateDTOList) {
            EvaluateDO evaluateDO = EvaluateConver.INSTANCE.convert(updateEvaluateDTO);
            FunUtil.defaultValue(evaluateDO, updateEvaluateDTO.getUserId(), false);
            count += evaluateMapper.updateById(evaluateDO);
        }
        return count;
    }

    public Integer updateProperty(List<UpdatePropertyDTO> updatePropertyDTOList) {
        Integer count = 0;
        for (UpdatePropertyDTO updatePropertyDTO : updatePropertyDTOList) {
            if (FunUtil.mustOne(updatePropertyDTO.getTeamLeaderSign(), updatePropertyDTO.getSafetyOfficerSign(), updatePropertyDTO.getStatus()))
                count += evaluateMapper.updateById(new EvaluateDO() {{
                    setId(updatePropertyDTO.getId());
                    FunUtil.defaultValue(this, updatePropertyDTO.getUserId(), false);
                    if (StrUtil.isNotBlank(updatePropertyDTO.getTeamLeaderSign())) {
                        setTeamLeaderSign(updatePropertyDTO.getTeamLeaderSign());
                        setLeaderSignDate(DateUtil.date());
                    }
                    if (StrUtil.isNotBlank(updatePropertyDTO.getSafetyOfficerSign())) {
                        setSafetyOfficerSign(updatePropertyDTO.getSafetyOfficerSign());
                        setOfficerSignDate(DateUtil.date());
                    }
                    if (StrUtil.isNotBlank(updatePropertyDTO.getStatus())) {
                        setStatus(Integer.valueOf(updatePropertyDTO.getStatus()));
                    }
                }});
        }
        return count;
    }

}
